// jsencrypt 文件别乱删 出问题会很难修复 由于jsencrypt使用了windows 小程序没有该对象所以新增了windows类
// 当前文件只在账号系统对接中使用
// #ifndef H5
import CryptoJS from 'crypto-js'
import JSEncrypt from './jsencrypt'
// #endif

// #ifdef H5
import JSEncrypt from 'jsencrypt'
import CryptoJS from 'crypto-es'
// #endif

const enpt: any = {}

const getAESkey = () => {
  let num = ''
  for (var i = 0; i < 16; i++) {
    num += Math.floor(Math.random() * 16).toString(16)
  }
  return num
}
// 密钥 16 位
let keystr = getAESkey()
// 初始向量 initial vector 16 位
let iv: any = 'E08ADE2699714B87'
enpt.string_key = getAESkey()
enpt.string_iv = getAESkey()
enpt.string_iv = 'E08ADE2699714B87'
let key = CryptoJS.enc.Utf8.parse(keystr)
iv = CryptoJS.enc.Utf8.parse(iv)

const NONCE = () => {
  let num = ''
  for (let i = 0; i < 6; i++) {
    num += Math.floor(Math.random() * 16).toString(16)
  }
  return num
}
// 6位密钥-帐号用
enpt.nonce = NONCE()

//AES加密
enpt.AESencrypt = (word: string, key: string) => {
  let srcs = CryptoJS.enc.Utf8.parse(word)
  let keys = CryptoJS.enc.Utf8.parse(key)
  let encrypted = CryptoJS.AES.encrypt(srcs, keys, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  })
  return encrypted.ciphertext.toString().toUpperCase()
}

//AES加密方法
enpt.Encrypt = (word: string) => {
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  })
  const result = CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
  const base64Data = CryptoJS.enc.Base64.parse(result)
  const HexData = CryptoJS.enc.Hex.stringify(base64Data)
  return HexData
}

//AES解密方法
enpt.Decrypt = (word: string | CryptoJS.lib.CipherParams) => {
  let decrypt = CryptoJS.AES.decrypt(word, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  })
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr.toString()
}
// RSA加密
enpt.RSAencrypt = (publicKey: string, key: any) => {
  let aeskey = JSON.stringify({ key: key })
  let encrypt = new JSEncrypt()
  encrypt.setPublicKey(publicKey)
  let encrypted = encrypt.encrypt(aeskey)
  return encrypted
}

// 数字信封加密
enpt.AESRSAencrypt = (str: any, publicKey: any) => {
  const aeskey = keystr
  const data = enpt.AESencrypt(str, aeskey)
  const key = enpt.RSAencrypt(publicKey, aeskey)
  return {
    data: data,
    key: key,
  }
}

// MD5
enpt.cryptomd5 = (word: string | CryptoJS.lib.WordArray) => {
  return CryptoJS.MD5(word).toString().toUpperCase()
}

// RSA解密
enpt.RSAdecrypt = (word: string | CryptoJS.lib.CipherParams) => {
  let ivStr = CryptoJS.enc.Utf8.parse('E08ADE2699714B87') // 此处后台iv为固定值
  let decrypt = CryptoJS.AES.decrypt(word, key, {
    iv: ivStr,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  })
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr.toString()
}

export default enpt
